﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using SilverlightHVP.ViewModel;

namespace SilverlightHVP.View
{
   public partial class SmoothStreamPlayer : UserControl
   {
      public SmoothStreamPlayer()
      {
         InitializeComponent();

         corePlayer.MediaOpened += new RoutedEventHandler(corePlayer_MediaOpened);
         corePlayer.MediaEnded += new RoutedEventHandler( corePlayer_MediaEnded );
         this.LayoutUpdated += new EventHandler(SmoothStreamPlayer_LayoutUpdated);
      }

      void corePlayer_MediaEnded( object sender, RoutedEventArgs e )
      {
         var pvm = this.DataContext as PlayerViewModel;
         pvm.VideoEnded();
         
      }

       
      void SmoothStreamPlayer_LayoutUpdated(object sender, EventArgs e)
      {
          var playerWidth = smoothPlayer.ActualWidth;
          if (!double.IsNaN(playerWidth))
          {
              smoothPlayer.Height = (int)(playerWidth / 16 * 9);
          }

         
      }

      public void SetViewModel( PlayerViewModel playerViewModel )
      {
         this.DataContext = playerViewModel;  
      }

      void corePlayer_MediaOpened(object sender, RoutedEventArgs e)
      {
          double width = corePlayer.NaturalVideoWidth;
      }
   }
}
